package player;

import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Iterator;

import utilPackage.Commons;



import main.Sprite;

public class Hammer extends Sprite implements Commons{

	ArrayList move_hisory;
	int degree;
	int hammer_rad;
	public int hammer_r;
	public ArrayList move_history;
	
	
	public Hammer(int player_x, int player_y, int l){
		
		life = l;
		move_history = new ArrayList();
		degree = 1;
		hammer_rad = 100;
		hammer_r = 15;
		
		int endX = (int) ((player_x  ) + ((hammer_rad) * Math.cos(((90 + degree) * (Math.PI / 180)))));
		int endY = (int) ((player_y  ) + ((hammer_rad) * Math.sin(((90.0 + degree) * (Math.PI / 180)))));
		
		x = endX;
		y = endY;
		
		add_to_move_history(x ,y);	
		
	}
	
	public void think(int player_x, int player_y){
		
		int old_degree = degree;
		degree += 15;
		life--;
		ArrayList tmp_move_history = new ArrayList();
		Iterator i = move_history.iterator();
		while(i.hasNext()){
			Sprite m = (Sprite) i.next();
			m.setLife(m.getLife() - 1);
			if(m.getLife() >= 1){
				tmp_move_history.add(m);				
			}			
		}
		move_history = tmp_move_history;
		
		int endX = (int) ((player_x ) + ((hammer_rad) * Math.cos(((90 + degree%361) * (Math.PI / 180)))));
		int endY = (int) ((player_y ) + ((hammer_rad) * Math.sin(((90 + degree%361) * (Math.PI / 180)))));
		
		x = endX;
		y = endY;
		
		for(int i1= 0; i1 <= 14; i1++){
			endX = (int) ((player_x ) + ((hammer_rad) * Math.cos(((90 + (old_degree+i1)%361) * (Math.PI / 180)))));
			endY = (int) ((player_y ) + ((hammer_rad) * Math.sin(((90 + (old_degree+i1)%361) * (Math.PI / 180)))));
			add_to_move_history(endX ,endY);	
			
		}
		
		
		
	}
	
	
	public void act(int player_x, int player_y){
		
		think(player_x, player_y);
		
		
	}
	
	public void add_to_move_history(int x, int y){
		
		/*Sprite s = new Sprite();
		s.setX(x);
		s.setY(y);
		s.setLife(HAMMER_TAIL);
		move_history.add(s);*/
		
	}
	
	public void drawHammer(Graphics g, int energy){
		
		int hammer_r = 15;
		
		double percent_energy = (double) (energy)/(double)(MAX_ENERGY);
		
		g.setColor(new Color(255, 255, 255, 50));
		g.fillOval(x + (hammer_r/2) -5, y + (hammer_r/2) -5 , hammer_r +10, hammer_r +10 );
		g.setColor(new Color(0, 255,255, Math.max(200, (int)(255*percent_energy))));
		g.fillOval(x + (hammer_r/2) -3, y + (hammer_r/2) -3 , hammer_r +6, hammer_r +6 );
		if(percent_energy >= .25){
			g.setColor(new Color(0, 0, Math.max(200,(int)(255*percent_energy)),255 ));
			g.fillOval(x + (hammer_r/2), y + (hammer_r/2), hammer_r, hammer_r );
		}
		
		
		Iterator s = move_history.iterator();
		while(s.hasNext()){
			Sprite m = (Sprite) s.next();
			double rand = Math.random();
			
			double percent_alive = (((double)(m.getLife()^2)) / (double)((HAMMER_TAIL)^2));
			double percent_alive2 = (((double)(m.getLife())/ (double)((HAMMER_TAIL))));
			double percent_tail_width = ((double)(hammer_r* percent_alive)/(double)hammer_r);
			double percent_tail_width2 = ((double)(hammer_r* percent_alive2)/(double)hammer_r);
			int smoke_size = (int)(hammer_r * percent_tail_width);
			int smoke_size2 = (int)(hammer_r * percent_tail_width2);
			int x_offset = (int)((hammer_r -  smoke_size )/2);
			int x = m.getX()+ x_offset;
			int y = m.getY() + hammer_r;
			
			
			g.setColor(new Color(255,255,255,(int)(20*percent_alive2)));	
			g.fillRoundRect(x, y, smoke_size2, 2, 0,0);
			g.setColor(new Color(0,0,0,(int)(20 *percent_alive2)));	
			g.fillRoundRect(x+4, y, smoke_size2-8, smoke_size2-8, 0,0);				
				
				
			g.setColor(new Color(0,0,255,(int)(20 * percent_alive2)));
			g.fillRoundRect(x, y, smoke_size2, 2, 5,5);
			//g.setColor(new Color(0,0,0,(int)(10 *(1 - percent_alive2))));	
			//g.fillRoundRect(x+4, y, smoke_size2-8, smoke_size2-8, 5,5);		
		}
		
	}

	@Override
	public void draw() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void think() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean doesCollide(Sprite s) {
		// TODO Auto-generated method stub
		return false;
	}

}
